#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2011/06/01

'''

from StringIO import StringIO
from UserCommentDetail import UserCommentDetail
from google.appengine.ext import db
import csv
import string

class UserCommentKindData(db.Model):
    commentKind = db.StringProperty(required=True, multiline=False)

class UserCommentKeywordData(db.Model):
    date = db.StringProperty(required=True, multiline=False)
    commentKind = db.ReferenceProperty(reference_class=UserCommentKindData)
    word1 = db.StringProperty(multiline=False)
    score1 = db.StringProperty(multiline=False)
    word2 = db.StringProperty(multiline=False)
    score2 = db.StringProperty(multiline=False)
    word3 = db.StringProperty(multiline=False)
    score3 = db.StringProperty(multiline=False)
    word4 = db.StringProperty(multiline=False)
    score4 = db.StringProperty(multiline=False)
    word5 = db.StringProperty(multiline=False)
    score5 = db.StringProperty(multiline=False)
    word6 = db.StringProperty(multiline=False)
    score6 = db.StringProperty(multiline=False)
    word7 = db.StringProperty(multiline=False)
    score7 = db.StringProperty(multiline=False)
    word8 = db.StringProperty(multiline=False)
    score8 = db.StringProperty(multiline=False)
    word9 = db.StringProperty(multiline=False)
    score9 = db.StringProperty(multiline=False)
    word10 = db.StringProperty(multiline=False)
    score10 = db.StringProperty(multiline=False)
    word11 = db.StringProperty(multiline=False)
    score11 = db.StringProperty(multiline=False)
    word12 = db.StringProperty(multiline=False)
    score12 = db.StringProperty(multiline=False)
    word13 = db.StringProperty(multiline=False)
    score13 = db.StringProperty(multiline=False)
    word14 = db.StringProperty(multiline=False)
    score14 = db.StringProperty(multiline=False)
    word15 = db.StringProperty(multiline=False)
    score15 = db.StringProperty(multiline=False)

class UserCommentKeyword:

    def outNotSelectedKindFromDB(self, kindString):
        return UserCommentKindData.gql('where commentKind != :1', kindString)
    def outCommentKeywordFromDB(self, kindString):
        kindDatas = UserCommentKindData.gql('WHERE commentKind = :1 ', kindString)
        for kindData in kindDatas:
            result = kindData.usercommentkeyworddata_set
            return result.order('-date')
        return None

    # udpate database
    def updateCommentKeyword(self, csvString, kindString):
        kinds = UserCommentKindData.gql('where commentKind = :1 ', kindString)
        kind = kinds.get()
        if kind == None:
            kind = UserCommentKindData(commentKind = kindString)
            kind.put()
        for csvData in csv.reader(StringIO(csvString)):
            date, word1, score1, word2, score2, word3, score3, word4, score4, word5, score5,\
            word6, score6, word7, score7, word8, score8, word9, score9, word10, score10, \
            word11, score11, word12, score12, word13, score13, word14, score14, word15, score15, \
            dummy\
            = csvData

            # すんげぇ馬鹿みたいなロジック。死にたい。
            keywordData = UserCommentKeywordData(date = date)
            keywordData.word1 = word1
            keywordData.score1 = score1
            keywordData.word2 = word2
            keywordData.score2 = score2
            keywordData.word3 = word3
            keywordData.score3 = score3
            keywordData.word4 = word4
            keywordData.score4 = score4
            keywordData.word5 = word5
            keywordData.score5 = score5
            keywordData.word6 = word6
            keywordData.score6 = score6
            keywordData.word7 = word7
            keywordData.score7 = score7
            keywordData.word8 = word8
            keywordData.score8 = score8
            keywordData.word9 = word9
            keywordData.score9 = score9
            keywordData.word10 = word10
            keywordData.word11 = word11
            keywordData.score11 = score11
            keywordData.word12 = word12
            keywordData.score12 = score12
            keywordData.word13 = word13
            keywordData.score13 = score13
            keywordData.word14 = word14
            keywordData.score14 = score14
            keywordData.word15 = word15
            keywordData.score15 = score15
            keywordData.commentKind = kind
            keywordData.put()

        return
    def deleteKeyword(self):
        datas = UserCommentKeywordData.all()
        for data in datas:
            data.delete()
        datas = UserCommentKindData.all()
        for data in datas:
            data.delete()
